package projectsupporter.form;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;

import projectsupporter.common.control.Path;
import projectsupporter.common.formbase.FolderFromWorkspaceDialog;
import projectsupporter.common.logic.GlobalInfo;
import projectsupporter.common.logic.PlugLogic;

public class ProjectInfoForm {

	private Shell parentShell;
	
	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private Composite composite = null;
	private Label label = null;
	private Button browseButton = null;
	private Text text = null;

	private Label label1 = null;

	private Button okButton = null;

	private Button importBaseButton = null;

	public ProjectInfoForm()
	{
		
	}
	
	public ProjectInfoForm(Shell parentShell)
	{
		this.parentShell = parentShell;
	}
	
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		if(parentShell==null)
		{
			sShell = new Shell();
		}
		else
		{
			sShell = new Shell(parentShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		}
		sShell.setText("Project Info");
		createComposite();
		sShell.setSize(new Point(512, 316));
		sShell.setLayout(new FillLayout());
	}

	/**
	 * This method initializes composite	
	 *
	 */
	private void createComposite() {
		composite = new Composite(sShell, SWT.NONE);
		composite.setLayout(null);
		label = new Label(composite, SWT.NONE);
		label.setText("Project : ");
		label.setBounds(new Rectangle(17, 40, 54, 18));
		text = new Text(composite, SWT.BORDER);
		text.setBounds(new Rectangle(75, 38, 311, 24));
		if(GlobalInfo.PRJ_NAME!=null)
		{
			text.setText(GlobalInfo.PRJ_NAME);
		}
		
		text.setEnabled(false);
		browseButton = new Button(composite, SWT.NONE);
		browseButton.setText("Change");
		browseButton.setLocation(new Point(397, 36));
		browseButton.setSize(new Point(87, 28));
		browseButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						doChose();
					}
				});
		label1 = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		label1.setBounds(new Rectangle(2, 217, 492, 18));
		label1.setText("Label");
		okButton = new Button(composite, SWT.NONE);
		okButton.setText("OK");
		okButton.setSize(new Point(87, 28));
		okButton.setLocation(new Point(397, 239));
		okButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				closeDialog();
			}
		});
		importBaseButton = new Button(composite, SWT.NONE);
		importBaseButton.setText("Import BaseCode");
		importBaseButton.setLocation(new Point(9, 239));
		importBaseButton.setSize(new Point(114, 28));
		importBaseButton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						doImportBase();
					}
				});
	}
	
	protected void doChose() {
		FolderFromWorkspaceDialog fd = new FolderFromWorkspaceDialog(sShell);
		fd.setPrjOnly(true);
		Path pt = fd.openFolderSelectDialog();	
		if(pt!=null)
		{
			GlobalInfo.PRJ_NAME=pt.getDisplayPath().trim();
			this.text.setText(GlobalInfo.PRJ_NAME);
		}
		
	}
	
	protected void doImportBase() {
		if(GlobalInfo.PRJ_NAME==null||GlobalInfo.PRJ_NAME.length()==0)
		{
			doChose();
		}
		if(GlobalInfo.PRJ_NAME!=null)
		{
			//PlugLogic.copyBaseCode(GlobalInfo.PRJ_NAME);
		}
	}

	/**
	 * openDialog
	 */
	public void openDialog() {
		final Display display = Display.getDefault();
		createSShell();// 初始化文件夹选择对话框
		sShell.open();// 打开文件夹选择窗口
		sShell.layout();
		while (!sShell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
//		display.dispose();
	}
	/**
	 * 关闭对话框
	 */
	protected void closeDialog() {
		if (sShell != null)
			sShell.dispose();// 释放底层的资源
	}
	/**
	 * Dialog test
	 */
	public static void main(String[] args) {
		ProjectInfoForm fd = new ProjectInfoForm(null);
		//fd.setPrjOnly(true);
		fd.openDialog();
	}
}
